package JAVAcollectionsanddatastructures.heap;

import java.util.*;

public class myheap {

    public int[] elem;
    public int usedSize;//数组的有效长度

    public myheap() {
        this.elem = new int[10];
    }


    //创建一个大堆
    public void createheap(int[] array) {
        for (int i = 0; i < usedSize; i++) {
            elem[i] = array[i];
            usedSize++;
        }
        for (int parent = ((usedSize - 1) - 1) / 2; parent >= 0; parent--) {
            shiftDown(parent, usedSize);
        }
    }

    //向下调整：调整是从最后一棵子树出发，且每棵子树都是向下调整
    public void shiftDown(int parent, int len) {//parent:每棵树的根节点，len：每棵树的结束位置（都是usedSize-1）
        int child = 2 * parent + 1;//默认左孩子为最大值
        //最起码有左孩子，
        while (child < len) {
            //判断是否有右孩子，并且找出最大值
            if (child + 1 < len && elem[child] < elem[child + 1]) {
                child++;//保证当前左右孩子最大值的下标
            }
            if (elem[child] > elem[parent]) {
                int temp = elem[child];
                elem[child] = elem[parent];
                elem[parent] = temp;
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }


    //入堆：向上调整
    public void offerheap(int value) {
        if (isFull()) {
            //满了就扩容
            elem = Arrays.copyOf(elem, 2 * elem.length);
        }
        elem[usedSize++] = value;
        shiftUp(usedSize - 1);
    }

    //判断数组是否满了
    public boolean isFull() {
        return usedSize == elem.length;
    }

    public void shiftUp(int child) {
        int parent = (child - 1) / 2;
        while (child > 0) {
            if (elem[child] > elem[parent]) {
                int tmp = elem[child];
                elem[child] = elem[parent];
                elem[parent] = tmp;
                child = parent;
                parent = (child - 1) / 2;
            } else {
                break;
            }
        }
    }


    //出堆：交换0下标元素与最后一个元素，然后向下调整
    public int pollheap() {
        if (isEmpty()) {
            throw new RuntimeException("优先级队列为空");
        }
        int tmp = elem[0];
        elem[0] = elem[usedSize - 1];
        elem[usedSize - 1] = tmp;

        usedSize--;
        shiftDown(0, usedSize);
        return tmp;
    }

    public boolean isEmpty() {
        return usedSize == 0;
    }


    //堆排序问题:先创建一个大根堆，再把0下标和最后一个未排序的元素进行交换，end--
    public void heapSort() {
        int end = usedSize - 1;
        while (end > 0) {
            int tmp = elem[0];
            elem[0] = elem[end];
            elem[end] = tmp;
            shiftDown(0, end);
            end--;
        }
    }


    //求数组当中前K个最小元素
    public static int[] topk(int[] array, int k) {
        //创建一个大小为k的大根堆
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        //遍历数组当中的元素，将前k个放入队列中
        for (int i = 0; i < array.length; i++) {
            if (maxHeap.size() < k) {
                maxHeap.offer(array[i]);
            } else {
                //从第k+1个元素开始，每个元素和堆顶元素比较
                int top = maxHeap.peek();
                if (top > array[i]) {
                    //先将堆顶元素弹出
                    maxHeap.poll();
                    //后存入
                    maxHeap.offer(array[i]);
                }
            }
        }
        int[] temp = new int[k];
        for (int i = 0; i < k; i++) {
            temp[i] = maxHeap.poll();
        }
        return temp;
    }

    public static void main(String[] args) {
        int[] array = {18, 21, 8, 10, 34, 12};
        int[] temp = topk(array, 3);
        System.out.println(Arrays.toString(temp));
    }


    //查找和最小的K对数字
    public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
        PriorityQueue<List<Integer>> maxheap = new PriorityQueue<>(k, new Comparator<List<Integer>>() {
            @Override
            public int compare(List<Integer> o1, List<Integer> o2) {
                return (o2.get(0) + o2.get(1)) - (o1.get(0) + o1.get(1));
            }
        });
        for (int i = 0; i < Math.min(nums1.length, k); i++) {
            for (int j = 0; j < Math.min(nums2.length, k); j++) {
                if (maxheap.size() < k) {
                    List<Integer> tempList = new ArrayList<>();
                    tempList.add(nums1[i]);
                    tempList.add(nums2[j]);
                    maxheap.offer(tempList);
                } else {
                    int top = maxheap.peek().get(0) + maxheap.peek().get(1);
                    if (top > nums1[i] + nums2[j]) {
                        maxheap.poll();
                        List<Integer> tempList = new ArrayList<>();
                        tempList.add(nums1[i]);
                        tempList.add(nums2[j]);
                        maxheap.offer(tempList);
                    }
                }
            }
        }
        List<List<Integer>> ret = new ArrayList<>();
        for (int i = 0; i < k && !maxheap.isEmpty(); i++) {
            ret.add(maxheap.poll());
        }
        return ret;
    }



}
